<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="objectName">
        <el-input
          v-model="queryParams.objectName"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="等级" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="请选择等级" clearable style="width: 80px">
          <el-option
            v-for="dict in met_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="英文分类" prop="secondClass">
        <el-input
          v-model="queryParams.secondClass"
          placeholder="请输入英文分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文分类" prop="secondClassCn">
        <el-input
          v-model="queryParams.secondClassCn"
          placeholder="请输入中文分类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['base:materials:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:materials:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:materials:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['base:materials:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            @click="openSyn=true"
        >同步</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialsList" @selection-change="handleSelectionChange" :show-overflow-tooltip="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="图片地址" align="center" prop="pic" width="250">
        <template #default="scope">
          <img :src="scope.row.pic" :style="{width:'75px',height:'auto',background: getColor(scope.row.grade)}" alt=""/>
        </template>
      </el-table-column>
      <el-table-column label="物品_id" align="center" prop="objectId" />
      <el-table-column label="名称" align="center" prop="objectName" />
      <el-table-column label="长度" align="center" prop="length" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="等级" align="center" prop="grade">
        <template #default="scope">
          <dict-tag :options="met_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="类型" align="center" prop="primaryClass">
        <template #default="scope">
          <dict-tag :options="primary_class" :value="scope.row.primaryClass"/>
        </template>
      </el-table-column>
      <el-table-column label="英文分类" align="center" prop="secondClass" />
      <el-table-column label="中文分类" align="center" prop="secondClassCn" />
      <el-table-column label="介绍" align="center" prop="desc" />
<!--      <el-table-column label="pre图片地址" align="center" prop="prePic" />-->
      <el-table-column label="平均价格" align="center" prop="avgPrice">
        <template #default="scope">
          {{ getPrice(scope.row.avgPrice) }}
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" align="center" prop="createDate" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:materials:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['base:materials:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品材料对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="materialsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物品_id" prop="objectId">
          <el-input v-model="form.objectId" placeholder="请输入物品_id" />
        </el-form-item>
        <el-form-item label="名称" prop="objectName">
          <el-input v-model="form.objectName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="长度" prop="length">
          <el-input v-model="form.length" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="等级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择等级">
            <el-option
              v-for="dict in met_grade"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="primaryClass" prop="primaryClass">
          <el-input v-model="form.primaryClass" placeholder="请输入primaryClass" />
        </el-form-item>
        <el-form-item label="英文分类" prop="secondClass">
          <el-input v-model="form.secondClass" placeholder="请输入英文分类" />
        </el-form-item>
        <el-form-item label="中文分类" prop="secondClassCn">
          <el-input v-model="form.secondClassCn" placeholder="请输入中文分类" />
        </el-form-item>
        <el-form-item label="介绍" prop="desc">
          <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="图片地址" prop="pic">
          <el-input v-model="form.pic" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="pre图片地址" prop="prePic">
          <el-input v-model="form.prePic" placeholder="请输入pre图片地址" />
        </el-form-item>
        <el-form-item label="平均价格" prop="avgPrice">
          <el-input v-model="form.avgPrice" placeholder="请输入平均价格" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="同步数据" v-model="openSyn" width="500px" append-to-body>
      <el-form ref="materialsRef" :model="SynForm" :rules="rulesSyn" label-width="80px">
        <el-form-item label="cookie" prop="cookie">
          <el-input type="textarea" v-model="SynForm.cookie" placeholder="cookie" />
        </el-form-item>
        <el-form-item label="params" prop="params">
          <el-input type="textarea" v-model="SynForm.params" placeholder="params" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormSyn">确 定</el-button>
          <el-button @click="openSyn=false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Materials">
import { listMaterials, getMaterials, delMaterials, addMaterials, updateMaterials, SysMaterials } from "@/api/base/materials"
import {getColor,getPrice} from "@/utils/sjz/common.js";

const { proxy } = getCurrentInstance()
const { met_grade,primary_class } = proxy.useDict('met_grade','primary_class')

const materialsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    objectName: null,
    grade: null,
    secondClass: null,
    secondClassCn: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询物品材料列表 */
function getList() {
  loading.value = true
  listMaterials(queryParams.value).then(response => {
    materialsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    objectId: null,
    objectName: null,
    length: null,
    width: null,
    grade: null,
    weight: null,
    primaryClass: null,
    secondClass: null,
    secondClassCn: null,
    desc: null,
    pic: null,
    prePic: null,
    avgPrice: null,
    createDate: null,
    updateDate: null
  }
  proxy.resetForm("materialsRef")
}
const openSyn = ref(false);
const SynForm = ref({
  cookie:"",
  params:""
})
const rulesSyn = {}

function submitFormSyn(){
  SysMaterials(SynForm.value).then(res=>{
    openSyn.value = false
    proxy.$modal.msgSuccess("同步成功")
  }).catch(err=>{
    proxy.$modal.msgError(err)
  })
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加物品材料"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMaterials(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改物品材料"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["materialsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMaterials(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMaterials(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除物品材料编号为"' + _ids + '"的数据项？').then(function() {
    return delMaterials(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('base/materials/export', {
    ...queryParams.value
  }, `materials_${new Date().getTime()}.xlsx`)
}

getList()
</script>
